package cn.edu.gdpt.mobile0123456789hwh0526;

import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {
    private EditText edtTxtMainName;
    private EditText edtTxtMainAge;
    private EditText edtTxtMainScore;
    private Button btnMainInsert;
    private Button btnMainUpdate;
    private Button btnMainDelete;
    private Button btnMainQurey;
    private TextView tvMainDisplay;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        btnMainInsert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//               获取输入数据
                String name=edtTxtMainName.getText().toString().trim();
                int age=Integer.valueOf(edtTxtMainAge.getText().toString());
                float score=Float.valueOf(edtTxtMainScore.getText().toString());
//                声明适配器对象
                ProviderAdapter adapter=new ProviderAdapter(MainActivity.this);
                adapter.insert(name,age,score);//调用添加数据方法
                Toast.makeText(MainActivity.this,"添加成功",Toast.LENGTH_LONG).show();
            }
        });
        btnMainQurey.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //声明适配器对象
                ProviderAdapter adapter=new ProviderAdapter(MainActivity.this);
                Cursor cursor=adapter.QueryAll();//查询数据
                if(!cursor.moveToFirst()){//判断是否为空
                    tvMainDisplay.setText("空");
                }else {
                    tvMainDisplay.setText(null);
                    do{//循环读取数据
                        String msg;
                        msg = "姓名:"+cursor.getString(1);
                        msg+="年龄:"+cursor.getInt(2);
                        msg+="分数:"+cursor.getFloat(3);
                        msg+="\n";
                        tvMainDisplay.append(msg);
                    }while (cursor.moveToNext());
                }
            }
        });
        btnMainUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取输入数据
                String name=edtTxtMainName.getText().toString().trim();
                int age=Integer.valueOf(edtTxtMainAge.getText().toString());
                float score=Float.valueOf(edtTxtMainScore.getText().toString());
                //声明适配器对象
                ProviderAdapter adapter=new ProviderAdapter(MainActivity.this);
                if(adapter.update(name,age,score)==1){
                    Toast.makeText(MainActivity.this,"更新成功",Toast.LENGTH_LONG).show();
                }
            }
        });
        btnMainDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name=edtTxtMainName.getText().toString().trim();
                //声明适配器对象
                ProviderAdapter adapter=new ProviderAdapter(MainActivity.this);
                if(adapter.delete(name)==1){
                    Toast.makeText(MainActivity.this,"删除成功",Toast.LENGTH_LONG).show();
                }
            }
        });
    }
    private void initView() {
        edtTxtMainName = (EditText) findViewById(R.id.edtTxtMainName);
        edtTxtMainAge = (EditText) findViewById(R.id.edtTxtMainAge);
        edtTxtMainScore = (EditText) findViewById(R.id.edtTxtMainScore);
        btnMainInsert = (Button) findViewById(R.id.btnMainInsert);
        btnMainUpdate = (Button) findViewById(R.id.btnMainUpdate);
        btnMainDelete = (Button) findViewById(R.id.btnMainDelete);
        btnMainQurey = (Button) findViewById(R.id.btnMainQurey);
        tvMainDisplay = (TextView) findViewById(R.id.tvMainDisplay);
    }
}